import {Component, ElementRef, Input, OnInit, ViewChild} from '@angular/core';

class MapNearbySearchModel {
  city?: String;
  location: String;
  type?: String;
  keyword?: String;
}

const AMap = window['AMap'];

@Component({
  selector: 'app-map-nearby',
  templateUrl: './map-nearby.component.html',
  styleUrls: ['./map-nearby.component.scss']
})
export class MapNearbyComponent implements OnInit {

  @ViewChild('container') container: ElementRef;
  _search: MapNearbySearchModel;

  map:any;
  marker:any;
  center:any;
  placeSearch:any;
  _geocoder:any;

  private get geocoder() {
    if (!this._geocoder) {
      this._geocoder = new AMap.Geocoder();
      // console.log('Init geocoder');
      // window['geocoder'] = this._geocoder;
    }
    return this._geocoder;
  }

  constructor() {
  }

  @Input()
  set search(v:any) {
    this._search = v;
    this.center = null;
    this.refresh();
  }

  get search() {
    return this._search;
  }

  ngOnInit() {
  }

  /**
   * 搜索周边
   */
  searchNearby(city: String, location: Array<Number>, type: String, keyword: String) {
    if (this.placeSearch == null) {
      // 构造地点查询类
      this.placeSearch = new AMap.PlaceSearch({
        pageSize: 10,
        pageIndex: 1,
        citylimit: true,
        map: this.map,
        showCover: false,
      });
    }
    if (type) {
      this.placeSearch.setType(type);
    }
    if (city) {
      this.placeSearch.setCity(city);
    }

    this.placeSearch.searchNearBy(keyword, location, 1000, function (status:any, result:any) {
      // console.log('Search Nearby', status, result);
    });
  }

  /**
   * 刷新地图
   */
  refresh() {
    if (this._search.location instanceof Array) {
      this.center = this._search.location;
    } else if (!this.center) {
      // 获取真实坐标
      this.geocoder.getLocation(this._search.location, (status:any, result:any) => {
        const geo = result.geocodes[0];
        if (status !== 'complete' || !geo) {
          // console.log(`Search no result for ${this._search.location}`, status, result);
          return;
        }
        // console.log(`Get real location for ${this._search.location} - ${geo.location.toString()}`);
        this.center = geo.location;
        // 再次触发刷新地图
        this.refresh();
      });
      return;
    }

    // 初始化地图
    let map = this.map;
    if (!map) {
      // console.log('Show map at center', this.center)
      map = new AMap.Map(this.container.nativeElement, {
        resizeEnable: true,
        zoom: 15,
        center: this.center,
      });
      this.marker = new AMap.Marker({
        position: this.center,
        map: map
      });
      AMap.plugin(['AMap.ToolBar', 'AMap.Scale'], function () {
        map.addControl(new AMap.ToolBar());
        map.addControl(new AMap.Scale());
      });
      this.map = map;
    }else {
      this.map.panTo(this.center);
      this.marker.setPosition(this.center);
    }

    const {city, type, keyword} = this._search;
    if (this.hasSearch()) {
      this.searchNearby(city, this.center, type, keyword);
    }
  }

  hasSearch() {
    const {city, type, keyword} = this._search;
    return type || keyword;
  }
}
